package commons.games;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :lyq
 * @description: TODO
 * @date :2019/10/20 16:30
 */
public class GameUtilsSt {
    public static int runningFlag = 0;
    public static int hanggingFlag = 0;
    private static String[][] mapFeatures = new String[5][4];
    private static Robot robot = null;
    public static List<String> pointsToHungUp = new ArrayList<String>();//挂机坐标点列表
    public static int movingTime = 5;//坐标点之间移动时间

    //地图特征
    static {
        mapFeatures[0] = new String[]{"627,20", "660,80", "703,195", "772,41"};//土城
        mapFeatures[1] = new String[]{"627,20", "660,80", "703,195", "772,41"};//蛇谷

        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    //小地图上坐标,和预计所用时间
    public static int moveTo(int x, int y, int estimate) {
        int tryCount = 0;
        try {
            do {
                tryCount++;
                Robot robot = new Robot();
                openOrCloseSmallMap();

                robot.mouseMove(x, y);
                robot.delay(300);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);

                openOrCloseSmallMap();
                robot.delay(estimate);
            } while (false);

        } catch (AWTException e) {
            e.printStackTrace();
        }
        return tryCount;
    }

    private static void openOrCloseSmallMap() {
        robot.delay(1000);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.delay(1000);
    }

    //点击
    public static int clickOn(int x, int y, int estimate) {
        int tryCount = 0;
        tryCount++;
        try {
            Robot robot = new Robot();
            robot.mouseMove(x, y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            robot.delay(estimate);
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return tryCount;
    }

    private static boolean isCheckedOkAfterMove(int x, int y, int estimate, int tryCount) {
        boolean flag = false;
        try {
            Robot robot = new Robot();
            List<Integer> listRgb = new ArrayList<Integer>();
            for (int i = 0; i < 10; i++) {
                Color pixelColor = robot.getPixelColor(x, y);
                int rgb = pixelColor.getRGB();//-3230315
                listRgb.add(rgb);
                robot.delay(100);
            }
            flag = listRgb.contains(-1);
            if (!listRgb.contains(-16712960) && !flag) {
                System.out.println("目标点[" + x + "," + y + "]无效 !");
            }
            if (listRgb.contains(-16712960) && !flag) {
                System.out.println("目标点[" + x + "," + y + "]有效,正在尝试移动...");
            }
            if (flag) {
                System.out.println("已到达目标点[" + x + "," + y + "] !");
                robot.delay(estimate);
            }
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return flag;
    }

    //跳图
    public static void switchMap(int from, int dest) {
        //土城到蛇谷
        if (from == 1 && dest == 2) {
            //beReadyToMove(from);
            //changePetStatus();
            //moveTo(658, 87,0);
            //clickOn(110, 14, 1000);
            //clickOn(28, 94, 1000);
            //clickOn(28, 94, 1000);
            //clickOn(29, 123, 1000);
            //runToLeftBottom(5000);
            //moveTo(740, 187,0);
            //runToLeftBottom(5000);

            //hangUpInBiqi();
        }else if(from == 1 && dest == 3){
            hangUp();
        }else{
            hangUp();
        }
    }

    private static void hangUp_() {
        //walkToBottom(1000);
        //walkToTop(1000);
        //walkToLeft(1000);
        //walkToRight(1000);
        //Point point = new Point(300,200);
        //attack(point);
        doAttack(5000);
        
    }

    private static void doAttack(int estimate) {
        int count = 0;
        boolean flag = false;
        do{
            count++;
            //寻找攻击范围内怪物位置
            List<Point> points = new ArrayList<Point>();
            int lastX = 0;
            int lastY = 0;
            robot.mouseMove(0,600);
            for(int x=200;x<500;x=x+20){
                for (int y = 30; y < 300; y++) {
                    Color color = robot.getPixelColor(x, y);
                    if(color.getRed()>100 && color.getGreen()==0 && color.getBlue()==0){
                        if((x==lastX && (y-lastY)<5)){
                            continue;
                        } else{
                            points.add(new Point(x,y+50));
                            lastX=x;
                            lastY=y;
                        }
                    }
                }
            }
            //筛选最近点
            int minDistance = 0;
            Point minPoint = new Point();
            for (int i = 0; i < points.size(); i++) {
                Point point = points.get(i);
                int temp = Math.abs(point.x-400)+Math.abs(point.y-180);
                if(i==0){
                    minDistance = temp;
                    minPoint = point;
                }else{
                    if(temp < minDistance){
                        minDistance = temp;
                        minPoint = point;
                    }
                }

            }
            //有怪物时,预留杀怪时间
            if(points.size() > 0){
                flag = true;
                robot.mouseMove(minPoint.x,minPoint.y);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
                robot.delay(estimate);
            }else{
                flag = false;
            }
        }while (count<8 && flag);
    }

    public static void hangUp() {
        while(true){
              for (int i = 0; i < pointsToHungUp.size(); i++) {
                String s = pointsToHungUp.get(i);
                String[] point = s.split(",");
                if(runningFlag == 1){
                    moveTo(Integer.valueOf(point[0]),Integer.valueOf(point[1]),3000);
                }
            }
        }
    }

    public static int curMapIndex() {
        List<Point> allWhitePointsInSmallMap = AllWhitePointsInSmallMap();
        int curIndex = 0;
        int lastCount = 0;
        for (int i = 0; i < mapFeatures.length; i++) {
            int sameCount = 0;
            String[] mapFeature = mapFeatures[i];
            for (int j = 0; j < mapFeature.length; j++) {
                if(mapFeature[j] == null){
                    continue;
                }
                String[] split = mapFeature[j].split(",");
                int x = Integer.valueOf(split[0]);
                int y = Integer.valueOf(split[1]);
                for (int k = 0; k < allWhitePointsInSmallMap.size(); k++) {
                    if (x == allWhitePointsInSmallMap.get(k).x && y == allWhitePointsInSmallMap.get(k).y) {
                        sameCount++;
                    }
                }
            }
            if (sameCount > lastCount){
                curIndex = i;
            }
        }
        return curIndex;
    }

    private static void runToLeftBottom(int estimate) {
        try {
            Robot robot = new Robot();
            robot.mouseMove(290, 270);
            robot.mousePress(InputEvent.BUTTON3_MASK);
            robot.delay(estimate);
            robot.mouseRelease(InputEvent.BUTTON3_MASK);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    private static void walkToLeft(int estimate) {
        try {
            Robot robot = new Robot();
            robot.mouseMove(200, 180);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            robot.delay(estimate);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    private static void walkToRight(int estimate) {
        try {
            Robot robot = new Robot();
            robot.mouseMove(540, 180);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            robot.delay(estimate);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    private static void walkToTop(int estimate) {
        try {
            Robot robot = new Robot();
            robot.mouseMove(400, 70);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            robot.delay(estimate);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    private static void walkToBottom(int estimate) {
        try {
            Robot robot = new Robot();
            robot.mouseMove(400, 300);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            robot.delay(estimate);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    private static void beReadyToMove(int mapIndex) {
        Point point = myLocationInSmallMap();
        while (point.x > 660 && point.x < 670 && point.y > 69 && point.y < 89) {
            try {
                Robot robot = new Robot();
                robot.keyPress(KeyEvent.VK_F4);
                robot.keyRelease(KeyEvent.VK_F4);

                point = myLocationInSmallMap();
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    }

    //小地图上所有白色点位
    public static List<Point> AllWhitePointsInSmallMap() {
        List<Point> listWhite = new ArrayList<Point>();
        try {
            Robot robot = new Robot();
            robot.mouseMove(0, 600);
            robot.delay(100);
            int lastX = 0, lastY = 0;
            for (int x = 600; x < 800; x++) {
                for (int y = 0; y < 200; y++) {
                    Color pixelColor = robot.getPixelColor(x, y);
                    if (pixelColor.getRGB() == -1) {
                        if (lastX == 0 && lastY == 0) {
                            listWhite.add(new Point(x, y));
                            lastX = x;
                            lastY = y;
                        } else {
                            if ((x - lastX) > 5 || (y - lastY) > 5) {
                                listWhite.add(new Point(x, y));
                                lastX = x;
                                lastY = y;
                            }
                        }
                    }
                }
            }
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return listWhite;
    }

    //分析并确定疑似位置
    public static Point maxSuspectedPint() {
        List<Point> suspectedPints = new ArrayList<Point>();
        List<Point> allWhitePointsInSmallMap = AllWhitePointsInSmallMap();
        for (int i = 0; i < allWhitePointsInSmallMap.size(); i++) {
            boolean isJump = false;
            for (int j = 0; j < mapFeatures[0].length; j++) {
                String[] split = mapFeatures[0][j].split(",");
                int x = Integer.valueOf(split[0]);
                int y = Integer.valueOf(split[1]);
                if (x == allWhitePointsInSmallMap.get(i).x && y == allWhitePointsInSmallMap.get(i).y) {
                    isJump = true;
                    break;
                }
            }
            //疑似位置
            if (!isJump) {
                suspectedPints.add(allWhitePointsInSmallMap.get(i));
            }
        }

        //排查疑似位置
        for (int i = 0; i < suspectedPints.size(); i++) {
            try {
                Robot robot = new Robot();
                List<Integer> listRgb = new ArrayList<Integer>();
                for (int j = 0; j < 10; j++) {
                    Color pixelColor = robot.getPixelColor(suspectedPints.get(i).x, suspectedPints.get(i).y);
                    int rgb = pixelColor.getRGB();
                    listRgb.add(rgb);
                    robot.delay(100);
                }
                if (listRgb.contains(-1)) {
                    return suspectedPints.get(i);
                }
            } catch (AWTException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    //当前小地图中人物位置
    public static Point myLocationInSmallMap() {
        Point curPoint = null;
        do {
            curPoint = maxSuspectedPint();
        } while (curPoint == null);
        return curPoint;
    }

    public static void changePetStatus(){
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_A);
            robot.delay(500);
            robot.keyRelease(KeyEvent.VK_A);
            robot.keyRelease(KeyEvent.VK_CONTROL);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}
